<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\sw_auth;
use App\sw_manager;
use App\sw_role;
class IndexController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $req)
    {

       
 //战略：admin_id---->role_id---->auth_ids
        //获得管理员session持久化的登录信息
        // $admin_id   = session('admin_id');
        $admin_name = $req->session()->get('mg_name');
        //① 管理员信息
        $admin_info = sw_manager::where('mg_name',$admin_name)->first();//SELECT * FROM `sw_manager` WHERE `mg_id` = 1
        $rold_id = $admin_info->mg_role_id;   //角色id信息
        //② 角色信息
        $role_info = sw_role::find($rold_id);//SELECT * FROM `sw_role` WHERE `role_id` = 50
        $auth_id = $role_info['auth_id'];
        $auth_ids=explode(',', $auth_id);

          //权限的ids信息
        //③ 全部权限信息
        //SELECT * FROM `sw_auth` WHERE `auth_id` IN ('101','104','102','107','109')
        if($admin_name==='admin'){
            //[admin超级管理员]要显示全部的权限
            //SELECT * FROM `sw_auth` WHERE ( auth_level=0 )
            $auth_infoA = sw_auth::where("auth_level",'1')->get();//顶级权限
            //SELECT * FROM `sw_auth` WHERE ( auth_level=1 )
            $auth_infoB = sw_auth::where("auth_level",'2')->get();;//次顶级权限
        }else{
            $auth_infoA = sw_auth::where("auth_level",'1') ->whereIn("auth_id",$auth_ids)->get();//顶级权限
            $auth_infoB = sw_auth::where("auth_level",'2') ->whereIn("auth_id",$auth_ids)->get();//次顶级权限
        }
        $name=$req->session()->get('mg_name');
            // dd($auth_infoA);
     return view('Admin.index',['authA'=>$auth_infoA,'authB'=>$auth_infoB,'name'=>$name]);
    }

  
}
